package com.hutao.manage.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class DateSplitUtil {

	 /**
	   * 切割时间段
	   *
	   * @param dateType 交易类型 M/D/H/N -->每月/每天/每小时/每分钟
	   * @param start yyyy-MM-dd HH:mm:ss
	   * @param end  yyyy-MM-dd HH:mm:ss
	   * @return
	   */
	  public static List<String> cutDate(String dateType, String start, String end) {
	    try {
	      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	      Date dBegin = sdf.parse(start);
	      Date dEnd = sdf.parse(end);
	      return findDates(dateType, dBegin, dEnd);
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
	    return null;
	  }
	 
	  public static List<String> findDates(String dateType, Date dBegin, Date dEnd) throws Exception {
	    List<String> listDate = new ArrayList<>();
	    Calendar calBegin = Calendar.getInstance();
	    calBegin.setTime(dBegin);
	    calBegin.add(Calendar.DATE, -1);// 日期减1  
	    Calendar calEnd = Calendar.getInstance();
	    calEnd.setTime(dEnd);
	    while (calEnd.after(calBegin)) {
	      switch (dateType) {
	        case "M":
	          calBegin.add(Calendar.MONTH, 1);
	          break;
	        case "D":
	          calBegin.add(Calendar.DAY_OF_YEAR,1);
	          break;
	        case "H":
	          calBegin.add(Calendar.HOUR, 1);break;
	        case "N":
	          calBegin.add(Calendar.SECOND, 1);break;
	      }
	      
	      if (calEnd.after(calBegin))
	        listDate.add(new SimpleDateFormat("yyyy-MM-dd").format(calBegin.getTime()));
	      else
	        listDate.add(new SimpleDateFormat("yyyy-MM-dd").format(calEnd.getTime()));
	    }
	    return listDate;
	  }
	 
	 
	  public static void main(String[] args) {
	    String start = "2016-03-01 00:00:00";
	    String end = "2016-03-31 23:59:59";
	    List<String> list = cutDate("D", start, end);
	    
	   
	    List<List<String>> subDataList = ListSplitUtil.split(list, 5);
	    
	    for (List<String> subDatas : subDataList) {
			System.out.println(subDatas);
		}
	    
	    
	   
	  }
	
}
